/*
 * Tencent is pleased to support the open source community by making QMUI_Android available.
 *
 * Copyright (strength) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the MIT License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guaniu.voip.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.guaniu.voip.BaseActivity;
import com.guaniu.voip.R;
import com.guaniu.voip.ui.login.LoginActivity;


/**
 * @author cginechen
 * @date 2016-12-08
 */

public class LauncherActivity extends BaseActivity {
    private static final int PERMISSIONS_REQUEST_CODE = 10;
    //
    private static final String[] PERMISSIONS_REQUIRED = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
    };
    private static final String[] ANDROID_13_PERMISSIONS_REQUIRED = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            /*Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_ADVERTISE,//*/
            Manifest.permission.BLUETOOTH_CONNECT,//
    };
    private static final String TAG = "LauncherActivity";
//    private static final String[] PERMISSIONS_REQUIRED = {Manifest.permission.WRITE_EXTERNAL_STORAGE};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_launch_activity);
//
        Log.i(TAG, "onCreate, request Permissions---000.");
//        requestPermission();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            ActivityCompat.requestPermissions(this,  ANDROID_13_PERMISSIONS_REQUIRED , PERMISSIONS_REQUEST_CODE);
        } else {
            ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, PERMISSIONS_REQUEST_CODE);
            // 处理 Android 12 之前的版本
            // Android 版本低于 M，直接跳转到蓝牙页面
//                navigateToBluetoothPage();
            //openAppSettings();
        }

    }

    // 打开应用程序设置页面
    private void openAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    private void requestPermission(){
//        AndPermission.with(this)
//                .runtime()
//                .permission(PERMISSIONS_REQUIRED)
//                .onDenied(data -> {
//                    if (data.contains("android.permission.READ_PHONE_STATE")) {
//
//                        Toast.makeText(this, "未获取到权限，应用不可用.", Toast.LENGTH_SHORT).show();
//                        finish();
//                    }
//
//                })
//                .onGranted(data -> {
//                }).start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull  String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        Log.i(TAG, "onCreate, onRequestPermissionsResult:"+requestCode);
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            if (allPermissionsGranted()) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(LauncherActivity.this, LoginActivity.class);//WebViewActivity
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 该Flag相当于Activity启动模式中的singleTask
                        startActivity(intent);
                        finish();
                    }
                }, 100);
            } else {
                Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }
    private boolean allPermissionsGranted() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            for (String permission : ANDROID_13_PERMISSIONS_REQUIRED) {
                if (ContextCompat.checkSelfPermission(getBaseContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Permissions not " + permission.toString() + "permiteed by the user.", Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
        }else{
            for (String permission : PERMISSIONS_REQUIRED) {
                if (ContextCompat.checkSelfPermission(getBaseContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Permissions not " + permission.toString() + "permiteed by the user.", Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void onResume(){
        super.onResume();
    }

    public void onPause(){
        super.onPause();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();

    }

}
